<template>
  <div>
    <ul class="oul">
      <li v-for="(item, index) in $store.state.list" :key="item.id">
        <div class="left">
          <img :src="item.img" alt="" />
        </div>
        <div class="right">
          <h5>
            {{ item.title }}
          </h5>
          <h4>
            {{ item.price.toFixed(2) }}
          </h4>
          <div>
            <button @click="addItem(index)">加</button>
          </div>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  created() {
    // 执行actions中的函数
    // commit  ==>  mutations
    // dispatch  ==>  actions
    this.$store.dispatch("init");
  },
  methods: {
    addItem(index) {
      this.$store.commit("addCarItem", this.$store.state.list[index])
    },
  },
};
</script>

<style scoped>
li {
  display: flex;
}

.left {
  flex: 1;
}

.left img {
  width: 100%;
}

.right {
  flex: 3;
}
</style>